package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.community.entity.RepairFollow;
import com.wanmait.community.mapper.AdminRepairMapper;
import com.wanmait.community.mapper.RepairFollowMapper;
import com.wanmait.community.service.RepairFollowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class RepairFollowServiceImpl extends ServiceImpl<RepairFollowMapper, RepairFollow> implements RepairFollowService{
    @Resource
    private RepairFollowMapper repairFollowMapper;
    @Resource
    private AdminRepairMapper adminRepairMapper;
    @Override
    public void insertRepairFollowByRepairFollow(RepairFollow repairFollow) {
        Date now = new Date();//给updateTime赋值，获取系统当前时间，
        repairFollow.setUpdateTime(now);
        repairFollowMapper.insertRepairFollowByRepairFollow(repairFollow);
        //加完一条回访记录，需要把他查出来获取对象，根据score判断是否修改adminRepair的维修状态
        RepairFollow insertRepairFollow = repairFollowMapper.selectById(repairFollow.getId());
        adminRepairMapper.updateFollowStatusById(repairFollow.getComplaintId());//修改回访状态为1已回访
        if(insertRepairFollow != null && insertRepairFollow.getScore()!=null){//判空，一般没啥用
           if(insertRepairFollow.getScore()<3) {//不满意，重置维修状态为未修理，重新去修
             adminRepairMapper.updateRepairStatus(insertRepairFollow.getComplaintId());
           }
        }
    }
    public RepairFollow selectByComplaintId(Integer complaintId) {
        return repairFollowMapper.selectByComplaintId(complaintId);
    }
    @Override
    public void updateRepairFollowByComplaintId(RepairFollow repairFollow) {
        Date now = new Date();
        repairFollow.setUpdateTime(now);//获取当前系统时间作为新的更新时间
        repairFollowMapper.updateRepairFollowByComplaintId(repairFollow);
    }
}
